# 
# #code republican, democrat, independent 
# data_panel$pid <- data_panel$pid3
# data_panel$pid[data_panel$pid>3] <- 3 #store other as independent
# data_panel$pid <- recode(data_panel$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
# data_panel$pid <- as.factor(data_panel$pid)
# data_panel$pid <-relevel(data_panel$pid, "Democrat") #code relative to dems
# 
# data_cross$pid <- data_cross$pid3
# data_cross$pid[data_cross$pid>3] <- 3 #store other as independent
# data_cross$pid <- recode(data_cross$pid, `1` = "Democrat", `2`= "Republican", `3` = "Independent")
# data_cross$pid <- as.factor(data_cross$pid)
# data_cross$pid <-relevel(data_cross$pid, "Democrat") #code relative to dems
# 
# #store leak and decision cut variables
# data_panel <- mutate(data_panel,
#                      leak_cut = case_when(
#                        wave > 0 ~ 1, #post-leak
#                        TRUE ~ 0
#                      ))
# 
# data_panel <- mutate(data_panel,
#                      decision_cut = case_when(
#                        wave > 1 ~ 1, #post-decision
#                        TRUE ~ 0
#                      ))
# 
# data_cross <- mutate(data_cross,
#                      leak_cut = case_when(
#                        wave > 0 ~ 1, #post-leak
#                        TRUE ~ 0
#                      ))
# 
# data_cross <- mutate(data_cross,
#                      decision_cut = case_when(
#                        wave > 1 ~ 1, #post-decision
#                        TRUE ~ 0
#                      ))

##### code state of residence ####

#1) does state have a trigger law? Y/N
data_cross$trigger_ban <- NA
data_cross$trigger_ban[!is.na(data_cross$state_policy_wording)] <- 0 #state does not have a trigger ban
data_cross$trigger_ban[data_cross$state_policy_wording < 14] <- 1 #state has a trigger ban


states <- unique(data_cross$inputstate)

for(i in 1:length(states)){
  data_cross$trigger_ban[data_cross$inputstate == states[i]] <- mean(data_cross$trigger_ban[data_cross$inputstate == states[i]], na.rm=T)
}

state_personal <- lm_robust(personal_roe ~ decision_cut *trigger_ban,
                            se_type = "HC1", 
                            #fixed_effects = universal_ID,
                            weights= weight,
                            data = data_cross)

state_norm <- lm_robust(norm ~ decision_cut *trigger_ban,
                        se_type = "HC1", 
                        #fixed_effects = universal_ID,
                        weights= weight,
                        data = data_cross)

state_opinion <- lm_robust(opinion ~ decision_cut *trigger_ban,
                           se_type = "HC1", 
                           #fixed_effects = universal_ID,
                           weights= weight,
                           data = data_cross)

screenreg(list(state_personal, state_norm, state_opinion), digits = 2, include.ci = TRUE, 
       stars = c(0.001, 0.01, 0.05),
       custom.model.names = c("Roe Should be Overturned", "Percieved American support for abortion", "Personal support for abortion"),
       caption.above = TRUE,
       custom.note = "*$p<.05$.",
       custom.coef.names= c( "Intercept","Decision", "Trigger Ban", "Decision X Trigger Ban" ))


modelsummary(list("Roe Should be Overturned"=state_personal, 
                  "Percieved American support for abortion"=state_norm,
                  "Personal support for abortion" = state_opinion),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             coef_rename = c("Intercept", "Decision", "Trigger Ban", "Decision x Trigger Ban"),
             output = "latex")

#2) did state go 1) blue or 2) purple or 3) red?
data_panel$state_color <- "blue" #blue state
data_panel$state_color[data_panel$inputstate == 1 | #AL
                         data_panel$inputstate == 2 | #AK
                         data_panel$inputstate == 5 | #AR
                         data_panel$inputstate == 16 | #ID
                         data_panel$inputstate == 18 | #IN
                         data_panel$inputstate == 21 | #KY
                         data_panel$inputstate == 20 | #KS
                         data_panel$inputstate == 22 | #LA
                         data_panel$inputstate == 28 | #MS
                         data_panel$inputstate == 29 | #MO
                         data_panel$inputstate == 30 | #MT
                         data_panel$inputstate == 31 | #NE
                         data_panel$inputstate == 38 | #ND
                         data_panel$inputstate == 40 | #OK
                         data_panel$inputstate == 45 | #SC
                         data_panel$inputstate == 46 | #SD
                         data_panel$inputstate == 49 | #UT 
                         data_panel$inputstate == 54 | #WV
                         data_panel$inputstate == 56 #WY
] <- "red" #red state

data_panel$state_color[data_panel$inputstate == 4 | #AZ
                         data_panel$inputstate == 12 | #FL
                         data_panel$inputstate == 13 | #GA
                         data_panel$inputstate == 19 | #IA
                         data_panel$inputstate == 23 | #ME
                         data_panel$inputstate == 26 | #MI
                         data_panel$inputstate == 27 | #MN
                         data_panel$inputstate == 32 | #NV
                         data_panel$inputstate == 33 | #NH
                         data_panel$inputstate == 37 | #NC
                         data_panel$inputstate == 39 | #OH
                         data_panel$inputstate == 42 | #PA
                         data_panel$inputstate == 48 | #TX
                         data_panel$inputstate == 55 #WI
] <- "purple" #purple state


data_cross$state_color <- "blue" #blue state
data_cross$state_color[data_cross$inputstate == 1 | #AL
                         data_cross$inputstate == 2 | #AK
                         data_cross$inputstate == 5 | #AR
                         data_cross$inputstate == 16 | #ID
                         data_cross$inputstate == 18 | #IN
                         data_cross$inputstate == 21 | #KY
                         data_cross$inputstate == 20 | #KS
                         data_cross$inputstate == 22 | #LA
                         data_cross$inputstate == 28 | #MS
                         data_cross$inputstate == 29 | #MO
                         data_cross$inputstate == 30 | #MT
                         data_cross$inputstate == 31 | #NE
                         data_cross$inputstate == 38 | #ND
                         data_cross$inputstate == 40 | #OK
                         data_cross$inputstate == 45 | #SC
                         data_cross$inputstate == 46 | #SD
                         data_cross$inputstate == 49 | #UT 
                         data_cross$inputstate == 54 | #WV
                         data_cross$inputstate == 56 #WY
] <- "red" #red state

data_cross$state_color[data_cross$inputstate == 4 | #AZ
                         data_cross$inputstate == 12 | #FL
                         data_cross$inputstate == 13 | #GA
                         data_cross$inputstate == 19 | #IA
                         data_cross$inputstate == 23 | #ME
                         data_cross$inputstate == 26 | #MI
                         data_cross$inputstate == 27 | #MN
                         data_cross$inputstate == 32 | #NV
                         data_cross$inputstate == 33 | #NH
                         data_cross$inputstate == 37 | #NC
                         data_cross$inputstate == 39 | #OH
                         data_cross$inputstate == 42 | #PA
                         data_cross$inputstate == 48 | #TX
                         data_cross$inputstate == 55 #WI
] <- "purple" #purple state


state_personal <- lm_robust(personal_roe ~ decision_cut *state_color,
                            se_type = "HC1", 
                            #fixed_effects = universal_ID,
                            weights= weight,
                            data = data_cross)

state_norm <- lm_robust(norm ~ decision_cut *state_color,
                        se_type = "HC1", 
                        #fixed_effects = universal_ID,
                        weights= weight,
                        data = data_cross)

state_opinion <- lm_robust(opinion ~ decision_cut *state_color,
                           se_type = "HC1", 
                           #fixed_effects = universal_ID,
                           weights= weight,
                           data = data_cross)

texreg(list(state_personal, state_norm, state_opinion), digits = 2, include.ci = TRUE, 
       stars = c(0.001, 0.01, 0.05),
       custom.model.names = c("Roe Should be Overturned", "Percieved American support for abortion", "Personal support for abortion"),
       caption.above = TRUE,
       custom.note = "*$p<.05$.",
       custom.coef.names= c( "Intercept","Decision", "Purple State", "Red State", "Decision X Red State", " Decision X Purple State"))

modelsummary(list("Roe Should be Overturned"=state_personal, 
                  "Percieved American support for abortion"=state_norm,
                  "Personal support for abortion" = state_opinion),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             coef_rename = c("Intercept", "Decision", "Purple State", "Red State", "Decision X Purple State", " Decision X Red State"),
             output = "latex")


# leak source the cross section: constitutionality 

prop.table(table(data_cross$info_source_sm))

info_source_sm_personal_roe <- lm_robust(personal_roe ~ info_source_sm, 
                                         se_type = "HC1", 
                                         weights= weight,
                                         data = data_cross[data_cross$wave == 1,])

info_source_cable_personal_roe <- lm_robust(personal_roe ~ info_source_cable, 
                                            se_type = "HC1", 
                                            weights= weight,
                                            data = data_cross[data_cross$wave == 1,])

info_source_local_personal_roe <- lm_robust(personal_roe ~ info_source_local, 
                                            se_type = "HC1", 
                                            weights= weight,
                                            data = data_cross[data_cross$wave == 1,])

info_source_online_personal_roe <- lm_robust(personal_roe ~ info_source_online, 
                                             se_type = "HC1", 
                                             weights= weight,
                                             data = data_cross[data_cross$wave == 1,])



screenreg(list(info_source_sm_personal_roe, info_source_cable_personal_roe, info_source_local_personal_roe, info_source_online_personal_roe), digits = 2, include.ci = TRUE, 
          stars = c(0.001, 0.01, 0.05),
          custom.model.names = c("Social Media", "Cable News", "Broadcast News", "Online News"),
          caption.above = TRUE, custom.note = "*$p<.05$.", 
          custom.coef.names= c("Intercept","Source: Social Media", "Source: Cable News", "Source: Broadcast News", "Source: Online News"))


modelsummary(list("Social Media"=info_source_sm_personal_roe, 
                  "Cable News"=info_source_cable_personal_roe, 
                  "Broadcast News"=info_source_local_personal_roe,
                  "Online News" = info_source_online_personal_roe),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             #coef_rename = c("Ruling"),
             output = "latex")

# leak source the cross section: norms

prop.table(table(data_cross$info_source_sm))

info_source_sm_norm <- lm_robust(norm ~ info_source_sm, 
                                 se_type = "HC1", 
                                 weights= weight,
                                 data = data_cross[data_cross$wave == 1,])

info_source_cable_norm <- lm_robust(norm~ info_source_cable, 
                                    se_type = "HC1", 
                                    weights= weight,
                                    data = data_cross[data_cross$wave == 1,])

info_source_local_norm <- lm_robust(norm ~ info_source_local, 
                                    se_type = "HC1", 
                                    weights= weight,
                                    data = data_cross[data_cross$wave == 1,])

info_source_online_norm <- lm_robust(norm ~ info_source_online, 
                                     se_type = "HC1", 
                                     weights= weight,
                                     data = data_cross[data_cross$wave == 1,])

screenreg(list(info_source_sm_norm, info_source_cable_norm, info_source_local_norm, info_source_online_norm), digits = 2, include.ci = TRUE, 
          stars = c(0.001, 0.01, 0.05),
          custom.model.names = c("Social Media", "Cable News", "Broadcast News", "Online News"),
          caption.above = TRUE, custom.note = "*$p<.05$.", 
          custom.coef.names= c("Intercept","Source: Social Media", "Source: Cable News", "Source: Broadcast News", "Source: Online News"))

modelsummary(list("Social Media"=info_source_sm_norm, 
                  "Cable News"=info_source_cable_norm, 
                  "Broadcast News"=info_source_local_norm,
                  "Online News" = info_source_online_norm),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             #coef_rename = c("Ruling"),
             output = "latex")

# leak source the cross section: opinion

prop.table(table(data_cross$info_source_sm))

info_source_sm_opinion <- lm_robust(opinion ~ info_source_sm, 
                                    se_type = "HC1", 
                                    weights= weight,
                                    data = data_cross[data_cross$wave == 1,])

info_source_cable_opinion <- lm_robust(opinion~ info_source_cable, 
                                       se_type = "HC1", 
                                       weights= weight,
                                       data = data_cross[data_cross$wave == 1,])

info_source_local_opinion <- lm_robust(opinion ~ info_source_local, 
                                       se_type = "HC1", 
                                       weights= weight,
                                       data = data_cross[data_cross$wave == 1,])

info_source_online_opinion <- lm_robust(opinion~ info_source_online, 
                                        se_type = "HC1", 
                                        weights= weight,
                                        data = data_cross[data_cross$wave == 1,])

texreg(list(info_source_sm_opinion, info_source_cable_opinion, info_source_local_opinion, info_source_online_opinion), digits = 2, include.ci = TRUE, 
       stars = c(0.001, 0.01, 0.05),
       custom.model.names = c("Social Media", "Cable News", "Broadcast News", "Online News"),
       caption.above = TRUE, custom.note = "*$p<.05$.", 
       custom.coef.names= c("Intercept","Source: Social Media", "Source: Cable News", "Source: Broadcast News", "Source: Online News"))

modelsummary(list("Social Media"=info_source_sm_opinion, 
                  "Cable News"=info_source_cable_opinion, 
                  "Broadcast News"=info_source_local_opinion,
                  "Online News" = info_source_online_opinion),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             #coef_rename = c("Ruling"),
             output = "latex")

# Cable news and the panel

id <- unique(data_panel$universal_ID)

for(i in 1:length(id)){
  data_panel$cable[data_panel$universal_ID == id[i]] <- mean(data_panel$cable[data_panel$universal_ID == id[i]], na.rm=T)
}

data_panel$cable <- data_panel$cable %>% recode('1' = 'CNN',
                                                '2'='MSNBC',
                                                '3'='FoxNews',
                                                '4'='Others',
                                                '5'='Do not watch cable')
data_panel$cable <- as.factor(data_panel$cable)
data_panel$cable <- fct_relevel(data_panel$cable, "Do not watch cable", after=0)


cable_personal_roe <- lm_robust(personal_roe ~ cable*leak_cut + cable*decision_cut, 
                                se_type = "HC1", 
                                weights= weight_ps,
                                data = data_panel)

cable_norm <- lm_robust(norm ~ cable*decision_cut, 
                        se_type = "HC1", 
                        weights= weight_ps,
                        data = data_panel)

cable_personal_dobbs <- lm_robust(opinion ~ cable*decision_cut, 
                                  se_type = "HC1", 
                                  weights= weight_ps,
                                  data = data_panel)

screenreg(list(cable_personal_roe, cable_norm, cable_personal_dobbs), digits = 2, include.ci = TRUE, 
          stars = c(0.001, 0.01, 0.05),
          custom.model.names = c("Roe Should be Overturned", "Percieved American support for abortion", "Personal support for abortion"),
          caption.above = TRUE,
          custom.note = "*$p<.05$.",
          custom.coef.names= c("Intercept","CNN", "FoxNews", "MSNBC", "Other Cable", "Leak", "Decision", "CNN X Leak", "FoxNews X Leak", "MSNBC X Leak", "Other Cable X Leak","CNN X Decision", "FoxNews X Decision", "MSNBC X Decision", "Other Cable X Decision"))

modelsummary(list("Roe Should be Overturned"=cable_personal_roe, 
                  "Percieved American support for abortion"=cable_norm, 
                  "Personal support for abortion"=cable_personal_dobbs),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             output = "latex")


for(i in 1:length(id)){
  data_panel$info_source_sm[data_panel$universal_ID == id[i]] <- mean(data_panel$info_source_sm[data_panel$universal_ID == id[i]], na.rm=T)
}



m1 <- lm_robust(personal_roe ~ info_source_sm* leak_cut, 
                se_type = "HC1", 
                weights= weight,
                data = data_panel)

m2 <- lm_robust(courtlegit_composite ~ info_source_sm* leak_cut, 
                se_type = "HC1", 
                weights= weight,
                data = data_panel)

screenreg(list(m1,m2), digits = 2, include.ci = TRUE, 
          stars = c(0.001, 0.01, 0.05),
          custom.model.names = c("Roe Should be Overturned", "Court Legitimacy"),
          caption.above = TRUE,
          custom.note = "*$p<.05$.",
          custom.coef.names= c("Intercept","Source: Social Media", "Leak", "Source: Social Media X Leak"))

modelsummary(list("Roe Should be Overturned"=m1, 
                  "Court Legitimacy"=m2),
             estimate  = "{estimate}",
             fmt = function(x) round(x, 3),
             statistic = "[{conf.low}, {conf.high}], p = {p.value}",
             output = "latex")
